package com.example.springbootjwt.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 夏虫言冰
 * @date 2021/4/28 21:52
 */
public class JWTutils {

    private  static  final String SIGN="0418";
    public String getToken(Map<String,String> map){
//        HashMap<String, Object> map = new HashMap<>();
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 1);
        JWTCreator.Builder builder = JWT.create();

        map.forEach((k,v)->{
            builder.withClaim(k,v);

        });
        builder.withExpiresAt(instance.getTime());
        String token = builder.sign(Algorithm.HMAC256(SIGN));
        System.out.println(token);
        return token;
    }

    public static  DecodedJWT verify(String token){

        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }

    public static  DecodedJWT geyTokenInfo(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        return verify;
    }



}
